package com.fantasy.ui;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Message;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.fantasy.R;
import com.fantasy.WimNotificationManager;
import com.fantasy.service.IRemoteService;
import com.fantasy.util.IMUtil;

public class MessageBoxActivity extends Activity implements OnClickListener {
	private static final String TAG = "MessageBoxActivity";
	private static final boolean DEBUG = true;

	public static final String MESSAGE_IP = "IP";
	public static final String MSG_CONTENT = "CONTENT";
	private String mIp;
	private String mContent;

	private TextView mTextView;
	private EditText mInput;
	private Button mSendBtn;

	private IRemoteService mService;
	private boolean mBound = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// setContentView(R.layout.message_show);
		setContentView(R.layout.messagebox_layout);
		// mWm = new WimNotificationManager(context);
		bindService(new Intent(IRemoteService.class.getName()), mConnection,
				Context.BIND_AUTO_CREATE);
		mBound = true;
		initView();

	}

	private void initView() {
		Intent intent = getIntent();
		mIp = intent.getStringExtra(MESSAGE_IP);
		mContent = intent.getStringExtra(MSG_CONTENT);
		mTextView = (TextView) findViewById(R.id.messagebox);
		if (mContent != null) {
			mTextView.setText(mContent);
		} else {
			mTextView.setText(this.getString(R.string.send_msg_to, mIp));
		}
		mInput = (EditText) findViewById(R.id.input_message);
		mSendBtn = (Button) findViewById(R.id.send);
		mSendBtn.setOnClickListener(this);

	}

	public void onClick(View v) {
		String sendMsg = mInput.getText().toString();
		if (mInput.getText() != null) {
			if (DEBUG) {
				Log.d(TAG, "onClick service send message:"
						+ mInput.getText().toString());
			}
			try {
				String msg = IMUtil.sendMessage(mIp, sendMsg);
				mService.sendMessage(msg);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
			unbindService(mConnection);
			mBound = false;
			finish();
		}
		if (DEBUG) {
			Log.d(TAG, "unbindService:");
		}

		if (DEBUG) {
			Log.d(TAG, "finish messagebox activity");
		}

	}

	private ServiceConnection mConnection = new ServiceConnection() {

		public void onServiceConnected(ComponentName name, IBinder service) {
			if (DEBUG) {
				Log.d(TAG, "onServiceConnected:");
			}
			if (DEBUG) {
				Log.d(TAG, "onServiceConnected:");
			}
			mService = IRemoteService.Stub.asInterface(service);

		}

		public void onServiceDisconnected(ComponentName name) {
			mService = null;
		}

	};

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		if (mBound)
			unbindService(mConnection);
	}

}
